package com.ellricky.baselib.ui.msg

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.adapter.MsgTipAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.new.MsgTip
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import kotlinx.android.synthetic.main.base_layout_rv.*
import okhttp3.Call
import java.util.*

class MsgTipChildFragment2 : BaseFragment<Void, BasePresenter<Void>>(), RvRefreshListener,
    AdapterView.OnItemClickListener {

    val adapter: MsgTipAdapter by lazy {
        MsgTipAdapter(index)
    }

    var refresh: Boolean = true
    var paramMap: MutableMap<String, String>? = mutableMapOf()
    var dataSource: MutableList<MsgTip> = mutableListOf()
    var index: Int = 0
    var type: String = ""

    companion object {
        fun newInstance(type: String, index: Int): MsgTipChildFragment2 {
            val fragment = MsgTipChildFragment2()
            val bundle = Bundle()
            bundle.putInt("index", index)
            bundle.putString(BaseConstant.KEY_TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.base_layout_rv
    }

    override fun initUIView(p0: View?) {
        index = arguments?.getInt("index") ?: 0
        type = arguments?.getString(BaseConstant.KEY_TYPE) ?: ""

        rvView.init(this)
        loadDataOnInit()
    }

    override fun setAdapter(rv: RecyclerView) {
        adapter.setOnItemClickListener(this)
        rv.adapter = adapter
    }

    override fun loadData(p0: Boolean) {
        this.refresh = p0
        val url = EAppUtil.getApiHost()
        initParamMap()
        paramMap?.apply {
            put("action", getAction())
            put("eid", LocalCacheUtil.getUid())
            put("school_id", LocalCacheUtil.getSchoolId())
            put("page_size", "40")
        }

        HttpUtils.getInstance().postFrom(url, paramMap,
            object : NewEListCallback<MsgTip>(mContext) {
                override fun onSuccess(data: MutableList<MsgTip>, msg: String?) {
                    this@MsgTipChildFragment2.dataSource = data
                    rvView.onLoadFinish(data, refresh)
                }

                override fun onErrorCode(code: String?, msg: String?) {
                    super.onErrorCode(code, msg)
                    rvView.onLoadFinish(dataSource, refresh)
                }

                override fun onError(call: Call?, e: Exception?, id: Int) {
                    super.onError(call, e, id)
                    rvView.onLoadFinish(dataSource, refresh)
                }
            })
    }

    private fun getAction(): String {
        when (type) {
            BaseConstant.CARD_TEACHER_MANAGER -> {
                return if (index == 0) {
                    "gzaBirthdayList"
                } else {
                    "gzaContinueList"
                }
            }
            BaseConstant.CARD_TEACHER -> {
                return if (index == 0) {
                    "teacherBirthdayList"
                } else {
                    "teacherContinueList"
                }
            }
        }
        return ""
    }

    private fun loadDataOnInit() {
        rvView.loadContent(true)
    }

    private fun initParamMap() {
        paramMap?.put("page", rvView.curPage.toString())
    }

    override fun updateAdapterData(p0: Boolean) {
        if (refresh) adapter.refresh(dataSource) else adapter.loadMore(dataSource)
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

    }
}